package com.ltmall.api.dao.impl;

import com.ltmall.api.bean.BaseEntity;
import com.ltmall.api.dao.IBaseEntityDAO;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

public class BaseEntityDAO<E extends BaseEntity> extends HibernateDaoSupport
        implements IBaseEntityDAO<E> {

    public void setInjectionSessionFacotry(SessionFactory sessionFacotry) {
        super.setSessionFactory(sessionFacotry);
    }

    public void injectSessionFactory() {
    }

    @Override
    public boolean persist(E entity) {
        entity.setOpTime(System.currentTimeMillis());
        System.out.printf("%s", entity);
        try {
            getHibernateTemplate().persist(entity);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean update(E entity) {
        entity.setOpTime(System.currentTimeMillis());
        System.out.printf("%s", entity);
        try {
            getHibernateTemplate().update(entity);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public void merge(E entity) {
        entity.setOpTime(System.currentTimeMillis());
        getHibernateTemplate().merge(entity);
    }

}
